<?php

namespace AppBundle\Controller;

use AppBundle\Entity\AdminPermission;
use AppBundle\Entity\AdminPermissionGroupMap;
use AppBundle\Entity\AdminPermissionGroups;
use AppBundle\Form\AdminPermissionGroupMapType;
use AppBundle\Form\AdminPermissionType;
use FOS\RestBundle\Controller\Annotations\Put;
use FOS\RestBundle\View\View;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Repository\AdminPermissionRepository;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

class PermissionController extends BaseController
{
    /**
     * @param Request $request
     *
     * @Route("/permission/add")
     * @Method({"GET","POST"})
     * @return Response
     */
    public function addAction(Request $request)
    {
        $permission = new AdminPermission();
        $form = $this->createForm(new AdminPermissionType(),$permission);
        $form->handleRequest($request);

        if($form->isSubmitted()&&$form->isValid())
        {
            $now = new \DateTime();
            $permission->setCreationDate($now);
            $permission->setModificationDate($now);
            $em = $this->getDoctrine()->getManager();
            $em->persist($permission);
            $em->flush();

            return new Response($permission->getId());
        }

        return $this->render('default/new.html.twig', array(
            'form' => $form->createView(),
        ));
    }

    /**
     * @param Request $request
     *
     * @Route("/permission/lists")
     * @Method({"GET"})
     * @return View
     */
    public function getPermissionAction(
        Request $request
    ){
        $permissionLists = $this->getDoctrine()->getRepository('AppBundle:AdminPermission')
            ->findAll();

       $view = new View();
       $view->setData(array(
           'lists'=>$permissionLists
       ));

       return $view;
    }

    /**
     * @param Request $request
     *
     * @Route("/permission")
     * @Method({"POST"})
     *
     * @return View
     */
    public function postPermissionAction(
        Request $request
    ){
        $permission = new AdminPermission();
        $form = $this->createForm(new AdminPermissionType(),$permission);
        $form->handleRequest($request);

        if(!$form->isValid()){
            throw new BadRequestHttpException('BAD Parameter');
        }

        $em = $this->getDoctrine()->getManager();

        $em->persist($permission);
        $em->flush();

        $this->postPermissionGroupMap($permission);

        return new View($permission->getId());
    }

    /**
     * @param Request $request
     * @param $id
     *
     * @Route("/permission/{id}")
     * @Method({"PUT"})
     */
    public function editPermission(
        Request $request,
        $id
    ) {
        $em = $this->getDoctrine()->getManager();
        $permission = $em->getRepository('AppBundle\Entity:AdminPermission')
            ->find($id);

        if(is_null($permission)){
            throw $this->createNotFoundException('Not found');
        }

        $form = $this->createForm(new AdminPermissionType(),$permission, array('method'=>'PUT'));
        if(!$form->isValid()){
            throw new BadRequestHttpException('Bad parameter');
        }

        $em->flush();

        $this->postPermissionGroupMap($permission);
    }

    /**
     * @param $permission
     * @return bool
     */
    private function postPermissionGroupMap(
        $permission
    ){
        $em = $this->getDoctrine()->getManager();

        $permissionId = $permission->getId();
        $permissionGroupId = $permission->getAdminPermissionGroup();
        $permissionGroupMap = $em->getRepository('AppBUndle:AdminPermissionGroupMap')
            ->findOneBy(array(
                'permissionId'=>$permissionId,
                'groupId'=>$permissionGroupId,
            ));

        if(!is_null($permissionGroupMap)){
            return ;
        }

        $permissionGruopMap = new AdminPermissionGroupMap();

        $permissionGruopMap->setGroupId($permissionGroupId);
        $permissionGruopMap->setPermissionId($permissionId);
        $em->persist($permissionGruopMap);
        $em->flush();

        return true;
    }
}